import babelpolyfill from 'babel-polyfill'
import router from './routes'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import { Message } from 'element-ui';
//import 'element-ui/lib/theme-default/index.css'
//import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-blue4/index.css'
import './assets/theme/theme-preview-blue/index.css'

import './assets/icon/iconfont.css'   //自定义图标

import Home from './views/Home.vue' //test for add route
import testvue from './views/deployment/testvue.vue'


// import VueRouter from 'vue-router'
import Vuex from 'vuex'
import store from './vuex/store'

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// import routes from './routes'

import { getToken,removeToken } from './utils/auth' // 验权


// import permission from './permission'    //权限控制

//import Mock from './mock'    //模拟后端接口，返回数据
//Mock.bootstrap();
import 'font-awesome/css/font-awesome.min.css'
import 'xterm/dist/xterm.css'

import VueCodemirror from 'vue-codemirror'
import 'codemirror/lib/codemirror.css'

//import BootstrapVue from 'bootstrap-vue'
//import 'bootstrap/dist/css/bootstrap.css'
//import 'bootstrap-vue/dist/bootstrap-vue.css'

Vue.use(ElementUI)
// Vue.use(VueRouter)
Vue.use(Vuex)
Vue.use(VueCodemirror)
//Vue.use(BootstrapVue)
//NProgress.configure({ showSpinner: false });

// const router = new VueRouter({
//   routes
//   //   routes: routes
// })


// function hasPermission(roles, permissionRoles) {
//     if (roles.indexOf('admin') >= 0) return true // admin permission passed directly
//     if (!permissionRoles) return true
//     return roles.some(role => permissionRoles.indexOf(role) >= 0)
// }

function hasPermission(roles) {
    if (roles.indexOf('admin') >= 0) return true // admin permission passed directly
    // if (!permissionRoles) return true
    // return roles.some(role => permissionRoles.indexOf(role) >= 0)
}

const whiteList = ['/login'] // 不重定向白名单



router.beforeEach((to, from, next) => {

    NProgress.start()
    if (getToken()) {
        console.log("flag is ..... ")
        console.log(store.state.user.isLogin)
            if (!store.state.user.isLogin) {


                store.dispatch('GetInfo').then(res => { // 拉取用户信息
                    console.log("res is ..............")
                    console.log(res)

                    // debugger;
                    store.commit('SET_LOGIN_IN')
                    store.commit('SAVE_USER_INFO',res.user)  //保存用户数据
                    store.commit('SAVE_USER_BTNLIMIT',res.btnLimit)  //保存用户按钮权限

                    if (res.user.role === "admin") {
                        router.options.routes=router.options.routes.concat(store.getters.getAdminRoutes)
                        // router.options.routes.push(store.getters.getTestRoutes)     //坑死老纸了
                        router.addRoutes(store.getters.getAdminRoutes)
                    } else if (res.user.role === "test") {
                        router.options.routes=router.options.routes.concat(store.getters.getTesterRoutes)
                        // router.options.routes.push(store.getters.getTestRoutes)     //坑死老纸了
                        router.addRoutes(store.getters.getTesterRoutes)
                    } else if (res.user.role === "dev")  {
                        router.options.routes=router.options.routes.concat(store.getters.getDevRoutes)
                        // router.options.routes.push(store.getters.getTestRoutes)     //坑死老纸了
                        router.addRoutes(store.getters.getDevRoutes)
                    }




                    next({ ...to, replace: true })
                    // next({ ...to })
                    // next()
                }).catch((err) => {
                    //后端登录接口错误或者Token已过期
                    console.log("token is guoqi..........")
                    removeToken()
                    store.commit('SET_LOGIN_OUT')
                    // this.$message.error('token已过期，请重新登录');
                    Message.error("Token已过期，请重新登录！")
                    // this.$router.go(0);  //清除store里面的值
                    // this.$router.push('/login');
                    router.go(0)
                    router.push('/login')

                    // store.dispatch('FedLogOut').then(() => {
                    //     Message.error(err || 'Verification failed, please login again')
                    //     next({ path: '/' })
                    // })
                })
            } else {
                next()
                // // 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
                // if (hasPermission(store.state.user.roles)) {
                //     next()//
                // } else {
                //
                //     // next({ path: '/401', replace: true, query: { noGoBack: true }})
                //     //跳转到无权限页面
                //     next({ path: '/403', replace: true, query: { noGoBack: true }})
                // }
            }

    } else {
        // // 设置浏览器头部标题
        // const browserHeaderTitle = to.name
        // // store.commit('SET_BROWSERHEADERTITLE', {
        // //     browserHeaderTitle: browserHeaderTitle
        // // })

        if (whiteList.indexOf(to.path) !== -1) {
            next()
        } else {
            next('/login')
            NProgress.done()
        }
    }
})



router.afterEach(() => {
    NProgress.done() // 结束Progress
    setTimeout(() => {
        const browserHeaderTitle = store.getters.browserHeaderTitle
        // setTitle(browserHeaderTitle)
    }, 0)
})



new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

